<template>
	<div class="content">
		<h2><i class="iconfont icon-tuikuantuihuo"></i>退换货审核</h2>
		<div class="content-wrap">
			<!--搜索分类-->
			<div class="search">
				<div class="search__list">
					<div class="search__list--title search__list--title-tip">审核类型(退货，换货)</div>
					<el-select v-model="params.return_type" @change="selctReturnGetState">
						<el-option label="全部" :value="0"></el-option>
						<el-option label="退货审核" :value="1"></el-option>
						<el-option label="换货审核" :value="2"></el-option>
					</el-select>
				</div>
				<div class="search__list">
					<div class="search__list--title search__list--title-tip">审核状态</div>
					<el-select v-model="params.return_state_id" @change="selctreturn_state_id">
						<el-option label="全部" :value="-1"></el-option>
						<el-option label="待审核" :value="1"></el-option>
						<el-option label="已完成" :value="9"></el-option>
						<el-option label="买家取消" :value="0"></el-option>
						<el-option label="卖家驳回" :value="2"></el-option>
						<el-option label="等待买家寄回退货" :value="3"></el-option>
						<el-option label="等待卖家收货" :value="4"></el-option>
						<el-option label="等待卖家发货" :value="5"></el-option>
						<el-option label="待收货" :value="6"></el-option>
						<el-option label="退款中" :value="7"></el-option>
						<el-option label="已退款" :value="8"></el-option>
					</el-select>
				</div>
				<div class="search__list">
					<div class="search__list--title">审核时间<small>&nbsp;(&nbsp;操作同意的时候的时间&nbsp;)&nbsp;</small></div>
					<el-date-picker
							@change="handleSelectTimes"
						    v-model="times"
						    type="daterange"
						    range-separator="至"
						    start-placeholder="开始日期"
						    end-placeholder="结束日期">
				  	</el-date-picker>
				</div>
			</div>

			<!--商品展示表格-->
			<div v-loading="loading" >
				<el-table :data="data" strip height="550" >
					<el-table-column label="退换货理由" align="center">
						<template slot-scope="scope">
							<el-tag v-if="scope.row.reason">{{ scope.row.reason.reason_name }}</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="申请日期" prop="cash_time">
						<template slot-scope="scope">
							<span>{{ scope.row.create_time | formatTime }}</span>
						</template>
					</el-table-column>
					<el-table-column label="审核日期" >
						<template slot-scope="scope">
							<span v-if="scope.row.agree_time">{{ scope.row.agree_time | formatTime }}</span>
						</template>
					</el-table-column>
					<el-table-column label="审核类型" align="center">
						<template slot-scope="scope">
							<el-tag type="danger" v-if="scope.row.return_type === 1">退货</el-tag>
							<el-tag type="warning" v-else-if="scope.row.return_type === 2">换货</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="状态" align="center">
						<template slot-scope="scope">
							<el-tag>{{scope.row.state.return_state_name}}</el-tag>
						</template>
					</el-table-column>
					
					<el-table-column label="操作" align="center" width="250">
						<template slot-scope="scope" size="small">
							<el-button 
								size="small"
								@click="agreeBackGoods(scope.row)"
								 type="success" 
								 v-if="scope.row.state.return_state_id === 1">
								 同意
							</el-button>
							<el-button 
								size="small"
								@click="rejectBackGoods(scope.row)"
								 type="danger" 
								 v-if="scope.row.state.return_state_id === 1">
								 拒绝
							</el-button>
							<el-button 
								size="small"
								@click="confirmGoods(scope.row.return_info_id)"
								 type="primary" 
								 v-if="scope.row.state.return_state_id === 4">
								 确认收货
							</el-button>
						</template>
					</el-table-column>
					<el-table-column label="退换货详情" align="center">
						<template slot-scope="scope" >
							<el-button type="text" @click="showDetail(scope.row.return_info_id)">查看</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<el-pagination 
				@current-change="handleCurrentChange"
				background 
				layout="total, prev, pager, next, jumper"
				:total="totalNum" 
				class="fr"
				>
			</el-pagination>
			<s-model v-if="showModel" :id="return_info_id"></s-model>
		</div>
	</div>
</template>

<script>
	
	import model from "./children/back_goods_detail.vue"
	export default {
		created() {
			this.fetchReturnGetList();
		},
		components: {
			"s-model": model
		},
		data() {
			return {
				params: {
					page: 1,
					rows: 10,
					start_time: "",
					end_time: "",
					return_type: 0,				//1代表退货2代表换货 ，0代表全部
					return_state_id: -1,		//
				},
				reject_reason: null,		//拒绝理由
				return_info_id: "", 		//查看详情时候传递的id值
				showModel: false,			//显示申请详情
				data: null,					//退换货详情		
				times: "",					//申请日期
				loading: true,				//加载小圈圈
				totalNum: 0					//返回数据总数，用于分页那里显示
			}
		},
		methods: {
			//翻页
			handleCurrentChange(pages) {
				this.params.page = pages;
				this.fetchReturnGetList();
			},
			//选择时间时候触发
			handleSelectTimes(val) {
				if (this.times) {
					this.params.start_time = this.times[0].getTime();
					this.params.end_time = this.times[1].getTime();
				}else {
					this.params.start_time = "";
					this.params.end_time = "";
				}
				this.fetchReturnGetList();

			},
			//选择审核状态0代表未审核，1代表已审核，空字符串代表全部
			selctreturn_state_id(val) {
				this.params.return_state_id = val;
				this.fetchReturnGetList();
			},
			//选择退换货状态0代表退货，1代表换货，空字符串代表全部
			selctReturnGetState(val) {
				this.params.applyType = val;
				this.fetchReturnGetList();
			},
			//获取退换货列表
			fetchReturnGetList() {
				this.loading = true;
				var url = `${this.url}/order/return_get_list_man.action`;
				var params = this.params;
				this.$http.get(url,{ params }).then((res)=> {
					this.loading = false;
					//console.log(res.data)
					this.data = res.data.result.rows;
					this.totalNum = res.data.result.total;
				});
			},
			//显示申请详情
			showDetail(id) {
				this.return_info_id = id;
				this.showModel = true;
			},
			//同意退换货
			agreeBackGoods(row) {
				this.loading = true;
				var url = `${this.url}/order/return_check_man.action`;
				var params = {
					return_info_id: row.return_info_id,
					is_agree: 1,
					reason: "",
				};
				this.$http.get(url,{ params }).then((res)=> {
					if( res.data.success ) {
						this.fetchReturnGetList();
					}else {
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
						this.loading = false;
					}
				});
			},
			//拒绝退换货
			rejectBackGoods(row) {
				this.$prompt("请填写拒绝理由", "拒绝申请", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					if (value === null || value.replace(/\s/g, "") === "") {
						this.$message({
							type: "error",
							message: "拒绝失败，理由不能为空"
						});
						return;
					}
					var url = `${this.url}/order/return_check_man.action`;
					var params = {
						return_info_id: row.return_info_id,
						is_agree: 0,
						reason: value,
					};
					this.$http.get(url,{ params }).then((res)=> {
						this.loading = true;
						if( res.data.success ) {
							this.fetchReturnGetList();
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
							this.loading = false;
						}
					});
				}).catch(()=>{
					
				});
			},
			//确认收货
			confirmGoods(id) {
				this.loading = true;
				var url = `${this.url}/order/return_receive_man.action`;
				var params = {
					return_info_id: id
				};
				this.$http.get(url,{ params }).then((res)=> {
					if( res.data.success ) {
						this.fetchReturnGetList();
					}else {
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
						this.loading = false;
					}
				});
			}
			
		}

	}
</script>

<style scoped lang="scss">

</style>